package com.hmall.service;

import com.hdmall.business.common.common.ResponseCode;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.Diction;
import com.hdmall.business.common.util.DateTimeUtil;
import com.hmall.util.PdfUtil;
import com.hmall.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

/**
 * 合同服务层
 */
@Service
public class ContService {
    private final DictionMapper dictionMapper;
    private final MallOrderMapper orderMapper;
    private final SysUserDeptMapper userDeptMapper;
    private final ContTempDeptRelMapper contTempDeptRelMapper;
    private final MallOrderSalesContMapper orderSalesContMapper;
    private final SysUserDetailMapper userDetailMapper;
    private final UpDownServiceImpl upDownService;
    private final UserService userService;
    private final MallOrderSalesContHistoryMapper salesContHistoryMapper;

    @Autowired
    public ContService(DictionMapper dictionMapper, MallOrderMapper orderMapper, SysUserDeptMapper userDeptMapper, ContTempDeptRelMapper contTempDeptRelMapper, MallOrderSalesContMapper orderSalesContMapper, SysUserDetailMapper userDetailMapper, UpDownServiceImpl upDownService, UserService userService, MallOrderSalesContHistoryMapper salesContHistoryMapper) {
        this.dictionMapper = dictionMapper;
        this.orderMapper = orderMapper;
        this.userDeptMapper = userDeptMapper;
        this.contTempDeptRelMapper = contTempDeptRelMapper;
        this.orderSalesContMapper = orderSalesContMapper;
        this.userDetailMapper = userDetailMapper;
        this.upDownService = upDownService;
        this.userService = userService;
        this.salesContHistoryMapper = salesContHistoryMapper;
    }

    //合同同意时--未付款状态-- 获取限时
    public ServerResponse<Long> salesContAgreeYesEndOrderTime(Integer userId,String contNo){
        MallOrderSalesContVo contVo;
        try {
            contVo= orderSalesContMapper.selectSalesContByContNo(userId,contNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_SELECT_NULL.getCode(),ResponseCode.ORDERSALE_CONT_SELECT_NULL.getDesc(),null);
        }
        Diction diction=null;
        try {
            diction=dictionMapper.selectByDictionKey("salesContYesEndTime");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DICTION_NOT_FOUND.getCode(),ResponseCode.DICTION_NOT_FOUND.getDesc(),null);
        }
        //改成30分钟超时测试
        Long time=DateTimeUtil.dateCompareMinute(contVo.getCreateTime(),30);
        //Long time=DateTimeUtil.dateCompare(contVo.getCreateTime(),Integer.parseInt(diction.getValue()));
        /*if(time<=0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_OUT_TIME.getCode(),ResponseCode.ORDER_OUT_TIME.getDesc(),time);
        }*/
        return ServerResponse.createByStatus(200,"获取合同限时OK",time);
    }
    //合同不同意时--合同失效状态-- 获取限时
    public ServerResponse<Long> salesContAgreeNoEndOrderTime(Integer userId,String contNo){
        MallOrderSalesContVo contVo=null;
        try {
            contVo= orderSalesContMapper.selectSalesContByContNo(userId,contNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_SELECT_NULL.getCode(),ResponseCode.ORDERSALE_CONT_SELECT_NULL.getDesc(),null);
        }
        Diction diction=null;
        try {
            diction=dictionMapper.selectByDictionKey("salesContNoEndTime");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DICTION_NOT_FOUND.getCode(),ResponseCode.DICTION_NOT_FOUND.getDesc(),null);
        }
        //改成30分钟超时测试
        Long time=DateTimeUtil.dateCompareMinute(contVo.getCreateTime(),30);
        //Long time=DateTimeUtil.dateCompare(contVo.getCreateTime(),Integer.parseInt(diction.getValue()));
        /*if(time<=0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_OUT_TIME.getCode(),ResponseCode.ORDER_OUT_TIME.getDesc(),time);
        }*/
        return ServerResponse.createByStatus(200,"获取订单限时OK",time);
    }
//用户查询合同11
   public ServerResponse<String> selectImgUrlContByContNo(Integer userId,String contNo){
       MallOrderSalesContVo cont= null;
       try {
           cont = orderSalesContMapper.selectSalesContByContNo(userId,contNo);
       } catch (Exception e) {
           e.printStackTrace();
           ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_SELECT_ERROR.getCode(),ResponseCode.ORDERSALE_CONT_SELECT_ERROR.getDesc(),null);
       }
       if(null==cont){
           ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_SELECT_NULL.getCode(),ResponseCode.ORDERSALE_CONT_SELECT_NULL.getDesc(),null);

       }
       assert cont != null;
       return  ServerResponse.createBySuccess(cont.getOrderSalesContPdf());
   }
    /**
     *  生成销售合同 显示pdfUrl
     * @param userId
     * @param orderNo
     * @return
     */
    public ServerResponse<String> pdfShowSalesContUrl(Integer userId,Long orderNo){
        //判断有没有这个合同 如果有取合同URL 合同编号
        /*MallOrderSalesContVo orderSalesContVo= orderSalesContMapper.selectSalesContByOderNo(orderNo);
        if(null!=orderSalesContVo){
            return ServerResponse.createBySuccess(orderSalesContVo.getOrderSalesContPdf());
        }*/
        //订单号查询订单和订单产品
        OrderSelectVo order=null;
        try {
            order=orderMapper.selectOrderByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_ERROR.getCode(),ResponseCode.ORDER_SELECT_ERROR.getDesc(),null) ;
        }
        if (null==order){
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        List<OrderSelectItemVo> itemList=null;
        try {
            itemList= orderMapper.selectOrderItemsByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        if(null==itemList||itemList.size()==0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        order.setOrderSelectItemVos(itemList);
        //用户id查询部门id
        SysDeptVo deptVo= null;
        try {
            deptVo = userDeptMapper.selectDeptByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==deptVo){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        //部门id查询条款
        List<ContClauseVo> contClauseVos= null;
        try {
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(deptVo.getDeptId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getDesc(),null) ;
        }

        if(null==contClauseVos||contClauseVos.size()==0){
            //如果沒有对应条款那么找公用模板
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(22);
            //return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }

        if(null==contClauseVos||contClauseVos.size()==0){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),"没有找到公用模板",null) ;
        }
        MallOrderSalesContVo orderSalesCont=new MallOrderSalesContVo();
        //订单与订单明细
        orderSalesCont.setOrderVo(order);
        orderSalesCont.setOrderNo(orderNo);
        orderSalesCont.setYiDeptId(deptVo.getDeptId());
        orderSalesCont.setYiDeptName(deptVo.getDeptName());
        //合同编号
        orderSalesCont.setSaleContNo(new Date().getTime()+"");
        orderSalesCont.setCreateTime(new Date());
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");

        //乙方联系
        UnitContactVo yiUnit;
        try {
            yiUnit= userDetailMapper.selectUnitByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.USER_NOT_FOUND.getCode(),ResponseCode.USER_NOT_FOUND.getDesc(),null) ;
        }
        yiUnit.setUnitName(deptVo.getDeptName());
        //甲方
        UnitContactVo jiaUnit=new UnitContactVo();
        jiaUnit.setUnitName("义乌华鼎锦纶股份有限公司");
        jiaUnit.setAddress("义乌市北苑街道雪峰西路751号");
        jiaUnit.setTel("0579-85261866");
        jiaUnit.setFax("0579-85261888");
        jiaUnit.setBankName("中国工商银行义乌分行");
        jiaUnit.setBankAccount("1208020009049099922");
        String fileName=deptVo.getDeptName()+"-"+orderSalesCont.getSaleContNo()+".pdf";

        orderSalesCont=PdfUtil.orderShowSalesContPdfUpload(fileName,"产品购销合同",orderSalesCont,yiUnit,jiaUnit);
        /*String fileName=orderSalesContVo.getOrderSalesContPdf();*/
        String pathPDF="pdf";
        String pathImg="img";
        File PDFFile=new File(pathPDF);
        File ImgFile=new File(pathImg);
        //如果文件夹不存在则创建
        if  (!PDFFile.exists()  && !PDFFile.isDirectory())
        {
            // System.out.println("//不存在");
            PDFFile.mkdir();
        }
        if  (!ImgFile.exists()  && !ImgFile.isDirectory())
        {
            // System.out.println("//不存在");
            ImgFile.mkdir();
        }
        //如果文件夹不存在则创建
        FileInputStream is= null;
        try {
            is = new FileInputStream(pathPDF+File.separator+fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //上传pdf
        String pdfUrl=upDownService.updatePDFHead(fileName,is);
        orderSalesCont.setOrderSalesContPdf(pdfUrl);
        /*String imgfileName= fileName.substring(0,fileName.lastIndexOf(".")+1)+"jpg";
        String pdf2img=IcepdfUtil.pdf2Img(pathPDF+File.separator+fileName,imgfileName);
        FileInputStream isImg= null;
        try {
            isImg = new FileInputStream(pdf2img);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
        //删除本地图片和pdf
        File PDFPath=new File(pathPDF+File.separator+fileName);
        if (PDFPath.exists()){
            PDFPath.delete();
        }
        /*try {
            orderSalesContMapper.insertOrderSalesCont(orderSalesCont);
        } catch (Exception e) {
            e.printStackTrace();
            return  ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getCode(),ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getDesc(),null);
        }*/
        System.out.println("图片上传成功！！");
        //setTableContClause(contClauseVos);
        //pdfWriteContClause("pdf/test.pdf","lllll",contClauseVos);
        /*OrderSalseUrlVo salseUrlVo= new OrderSalseUrlVo();
        salseUrlVo.setSalesContImgUrl(imgNameUrl);
        salseUrlVo.setSalesContNo(orderSalesCont.getSaleContNo());*/
        return ServerResponse.createBySuccess(pdfUrl);

    }
    /**
     * 生成销售合同 显示imgurl
     * @param userId
     * @param orderNo
     * @return
     */
    public ServerResponse<String> showSalesContUrl(Integer userId,Long orderNo){
        //判断有没有这个合同 如果有取合同URL 合同编号
        /*MallOrderSalesContVo orderSalesContVo= orderSalesContMapper.selectSalesContByOderNo(orderNo);
        if(null!=orderSalesContVo){
            return ServerResponse.createBySuccess(orderSalesContVo.getOrderSalesContPdf());
        }*/
        //订单号查询订单和订单产品
        OrderSelectVo order=null;
        try {
            order=orderMapper.selectOrderByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_ERROR.getCode(),ResponseCode.ORDER_SELECT_ERROR.getDesc(),null) ;
        }
        if (null==order){
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        List<OrderSelectItemVo> itemList=null;
        try {
            itemList= orderMapper.selectOrderItemsByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        if(null==itemList||itemList.size()==0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        order.setOrderSelectItemVos(itemList);
        //买方用户id查询部门
        SysDeptVo deptBuyerVo=userService.findBuyerDept(userId).getData();

        //部门id查询条款
        List<ContClauseVo> contClauseVos= null;
        try {
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(deptBuyerVo.getDeptId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==contClauseVos||contClauseVos.size()==0){
            //如果沒有对应条款那么找公用模板
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(22);
            //return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==contClauseVos||contClauseVos.size()==0){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),"没有找到公用模板",null) ;
        }

        MallOrderSalesContVo orderSalesCont=new MallOrderSalesContVo();
        //订单与订单明细
        orderSalesCont.setOrderVo(order);
        orderSalesCont.setOrderNo(orderNo);
        orderSalesCont.setYiDeptId(deptBuyerVo.getDeptId());
        orderSalesCont.setYiDeptName(deptBuyerVo.getDeptName());
        //合同编号
        orderSalesCont.setSaleContNo(new Date().getTime()+"");
        orderSalesCont.setCreateTime(new Date());
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");

        //乙方联系
        UnitContactVo yiUnit;
        try {
            yiUnit= userDetailMapper.selectUnitByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.USER_NOT_FOUND.getCode(),ResponseCode.USER_NOT_FOUND.getDesc(),null) ;
        }
        yiUnit.setUnitName(deptBuyerVo.getDeptName());
        //甲方
        UnitContactVo jiaUnit=new UnitContactVo();
        jiaUnit.setUnitName("义乌华鼎锦纶股份有限公司");
        jiaUnit.setAddress("义乌市北苑街道雪峰西路751号");
        jiaUnit.setTel("0579-85261866");
        jiaUnit.setFax("0579-85261888");
        jiaUnit.setBankName("中国工商银行义乌分行");
        jiaUnit.setBankAccount("1208020009049099922");
        String fileName=deptBuyerVo.getDeptName()+"-"+orderSalesCont.getSaleContNo()+".pdf";

        orderSalesCont=PdfUtil.orderShowSalesContPdfUpload(fileName,"产品购销合同",orderSalesCont,yiUnit,jiaUnit);
        /*String fileName=orderSalesContVo.getOrderSalesContPdf();*/
        String pathPDF="pdf";
        File PDFFile=new File(pathPDF);
        //如果文件夹不存在则创建
        if  (!PDFFile.exists()  && !PDFFile.isDirectory())
        {
            // System.out.println("//不存在");
            PDFFile.mkdir();
        }
        //如果文件夹不存在则创建
        FileInputStream is= null;
        try {
            is = new FileInputStream(pathPDF+File.separator+fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //上传pdf
        String pdfUrl=upDownService.updatePDFHead(fileName,is);
        orderSalesCont.setOrderSalesContPdf(pdfUrl);
        //删除本地图片和pdf
        File PDFPath=new File(pathPDF+File.separator+fileName);
        if (PDFPath.exists()){
            PDFPath.delete();
        }
        System.out.println("图片上传成功！！");
        return ServerResponse.createBySuccess(pdfUrl);

    }

    /**
     * 生成销售合同 同意或是不同意
     * @param userId
     * @param orderNo
     * @return
     */
    public ServerResponse<OrderSalseUrlVo> salesContIsAgree(Integer userId,Long orderNo,Integer isAgree){
        //订单号查询订单和订单产品
        OrderSelectVo order=null;
        try {
            order=orderMapper.selectOrderByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_ERROR.getCode(),ResponseCode.ORDER_SELECT_ERROR.getDesc(),null) ;
        }
        if (null==order){
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        List<OrderSelectItemVo> itemList=null;
        try {
            itemList= orderMapper.selectOrderItemsByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        if(null==itemList||itemList.size()==0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        order.setOrderSelectItemVos(itemList);
        //买方用户id查询部门id
        SysDeptVo deptBuyerVo=userService.findBuyerDept(userId).getData();
        //部门id查询条款
        List<ContClauseVo> contClauseVos= null;
        try {
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(deptBuyerVo.getDeptId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==contClauseVos||contClauseVos.size()==0){
            //如果沒有对应条款那么找公用模板
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(22);
            //return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==contClauseVos||contClauseVos.size()==0){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),"没有找到公用模板",null) ;
        }

        MallOrderSalesContVo orderSalesCont=new MallOrderSalesContVo();
        //订单与订单明细
        orderSalesCont.setIsAgree(isAgree);
        orderSalesCont.setOrderVo(order);
        orderSalesCont.setOrderNo(orderNo);
        orderSalesCont.setYiDeptId(deptBuyerVo.getDeptId());
        orderSalesCont.setYiDeptName(deptBuyerVo.getDeptName());
        //合同编号
        orderSalesCont.setSaleContNo(new Date().getTime()+"");
        orderSalesCont.setCreateTime(new Date());
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");
        //列表显示产品与条款
        orderSalesCont.setContClauseVos(contClauseVos);
        //orderSalesContVo.setDissent(dissent);
        orderSalesCont.setUserId(userId);
        orderSalesCont.setJiaDeptName("华鼎股份");

        //乙方联系
        UnitContactVo yiUnit;
        try {
            yiUnit= userDetailMapper.selectUnitByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.USER_NOT_FOUND.getCode(),ResponseCode.USER_NOT_FOUND.getDesc(),null) ;
        }
        yiUnit.setUnitName(deptBuyerVo.getDeptName());
        //甲方
        UnitContactVo jiaUnit=new UnitContactVo();
        jiaUnit.setUnitName("义乌华鼎锦纶股份有限公司");
        jiaUnit.setAddress("义乌市北苑街道雪峰西路751号");
        jiaUnit.setTel("0579-85261866");
        jiaUnit.setFax("0579-85261888");
        jiaUnit.setBankName("中国工商银行义乌分行");
        jiaUnit.setBankAccount("1208020009049099922");
        String fileName=deptBuyerVo.getDeptName()+"-"+orderSalesCont.getSaleContNo()+".pdf";

        orderSalesCont=PdfUtil.orderCreateSalesContPdfUpload(fileName,"产品购销合同",orderSalesCont,yiUnit,jiaUnit);
        String pathPDF="pdf";
        File PDFFile=new File(pathPDF);
        //如果文件夹不存在则创建
        if  (!PDFFile.exists()  && !PDFFile.isDirectory())
        {
            // System.out.println("//不存在");
            PDFFile.mkdir();
        }
        //如果文件夹不存在则创建
        FileInputStream is= null;
        try {
            is = new FileInputStream(pathPDF+File.separator+fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //上传pdf
        String pdfUrl=upDownService.updatePDFHead(fileName,is);
        orderSalesCont.setOrderSalesContPdf(pdfUrl);
        //删除本地图片和pdf
        File PDFPath=new File(pathPDF+File.separator+fileName);
        if (PDFPath.exists()){
            PDFPath.delete();
        }

        try {
            orderSalesContMapper.deleteContByOderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return  ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_DELETE_ERROR.getCode(),ResponseCode.ORDERSALE_CONT_DELETE_ERROR.getDesc(),null);
        }
        try {
            orderSalesContMapper.insertOrderSalesCont(orderSalesCont);
        } catch (Exception e) {
            e.printStackTrace();
            return  ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getCode(),ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getDesc(),null);
        }
        //如果同意 订单状态未付款30
        if (isAgree==1){
            try {
                 orderMapper.updateByOrderNo(30,orderNo);
            } catch (Exception e) {
                e.printStackTrace();
                return  ServerResponse.createByStatus(ResponseCode.ORDER_UPDATE_ERROR.getCode(),ResponseCode.ORDER_UPDATE_ERROR.getDesc(),null);
            }
        }else{
            try {
            //不同意 订单状态合同失效36
                orderMapper.updateByOrderNo(36,orderNo);
            } catch (Exception e) {
                e.printStackTrace();
                return  ServerResponse.createByStatus(ResponseCode.ORDER_UPDATE_ERROR.getCode(),ResponseCode.ORDER_UPDATE_ERROR.getDesc(),null);
            }
        }
        //保留历史记录
        salesContHistoryMapper.insertContHistory(orderSalesCont);
        OrderSalseUrlVo salseUrlVo= new OrderSalseUrlVo();
        salseUrlVo.setSalesContImgUrl(pdfUrl);
        salseUrlVo.setSalesContNo(orderSalesCont.getSaleContNo());
        return ServerResponse.createBySuccess(salseUrlVo);

    }

    /**
     * 定时任务 同意合同超时设置订单 未签合同35 不同意超时0-->
     */
    public void updateTaskAgreeContTimeout(String itemId){

        Diction dictionYes= null;
        Diction dictionNo= null;
        if("0".equals(itemId)){
            try {
                dictionYes = dictionMapper.selectByDictionKey("salesContYesEndTime");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("字典查询有误");
            }
            assert dictionYes != null;
            Integer endYes=Integer.parseInt(dictionYes.getValue());
            //合同--同意--超时 状态 未签合同35
            orderSalesContMapper.agreeNoContOutTime(endYes);
            System.out.println("itemid:"+itemId);
        }else{
            try {
                dictionNo = dictionMapper.selectByDictionKey("salesContNoEndTime");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("字典查询有误");
            }
            assert dictionNo != null;
            Integer endNo=Integer.parseInt(dictionNo.getValue());
            //合同--不同意--超时 状态0 取消
            orderSalesContMapper.agreeYesContOutTime(endNo);
            System.out.println("itemid:"+itemId);
        }

    }
    /* public ServerResponse<String> updateSalesCont(Integer userId,Long orderNo){
        //订单号查询订单和订单产品
        OrderSelectVo order=null;
        try {
            order=orderMapper.selectOrderByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_ERROR.getCode(),ResponseCode.ORDER_SELECT_ERROR.getDesc(),null) ;
        }
        if (null==order){
            return ServerResponse.createByStatus(ResponseCode.ORDER_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        List<OrderSelectItemVo> itemList=null;
        try {
            itemList= orderMapper.selectOrderItemsByOrderNo(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        if(null==itemList||itemList.size()==0){
            return ServerResponse.createByStatus(ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getCode(),ResponseCode.ORDER_ITEM_SELECT_NOT_ERROR.getDesc(),null) ;
        }
        order.setOrderSelectItemVos(itemList);
        //用户id查询部门id
        SysDeptVo deptVo= null;
        try {
            deptVo = userDeptMapper.selectDeptByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==deptVo){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        //部门id查询条款
        List<ContClauseVo> contClauseVos= null;
        try {
            contClauseVos = contTempDeptRelMapper.selectContClauseVoByDeptId(deptVo.getDeptId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_CONT_ISNULL_ERROR.getDesc(),null) ;
        }
        if(null==contClauseVos||contClauseVos.size()==0){
            return ServerResponse.createByStatus(ResponseCode.DEPT_SELECT_ISNULL_ERROR.getCode(),ResponseCode.DEPT_SELECT_ISNULL_ERROR.getDesc(),null) ;
        }
        //列表显示产品与条款
        MallOrderSalesContVo orderSalesContVo=new MallOrderSalesContVo();
        orderSalesContVo.setContClauseVos(contClauseVos);
        //订单与订单明细
        orderSalesContVo.setOrderVo(order);
        orderSalesContVo.setOrderNo(orderNo);
        orderSalesContVo.setYiDeptId(deptVo.getDeptId());
        orderSalesContVo.setYiDeptName(deptVo.getDeptName());
       // orderSalesContVo.setDissent(dissent);
        orderSalesContVo.setUserId(userId);
        orderSalesContVo.setJiaDeptName("华鼎股份");
        //合同编号
        orderSalesContVo.setSaleContNo(new Date().getTime()+"");
        orderSalesContVo.setCreateTime(new Date());
        //乙方联系
        UnitContactVo yiUnit;
        try {
            yiUnit= userDetailMapper.selectUnitByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByStatus(ResponseCode.USER_NOT_FOUND.getCode(),ResponseCode.USER_NOT_FOUND.getDesc(),null) ;
        }
        yiUnit.setUnitName(deptVo.getDeptName());
        //甲方
        UnitContactVo jiaUnit=new UnitContactVo();
        jiaUnit.setUnitName("义乌华鼎锦纶股份有限公司");
        jiaUnit.setAddress("义乌市北苑街道雪峰西路751号");
        jiaUnit.setTel("0579-85261866");
        jiaUnit.setFax("0579-85261888");
        jiaUnit.setBankName("中国工商银行义乌分行");
        jiaUnit.setBankAccount("1208020009049099922");
        String fileName=deptVo.getDeptName()+"-"+orderSalesContVo.getSaleContNo()+".pdf";

        orderSalesContVo=PdfUtil.orderSalesContPdfUpload(fileName,"产品购销合同",orderSalesContVo,yiUnit,jiaUnit);
        *//*String fileName=orderSalesContVo.getOrderSalesContPdf();*//*
        String pathPDF="pdf";
        String pathImg="img";
        File PDFFile=new File(pathPDF);
        File ImgFile=new File(pathImg);
        //如果文件夹不存在则创建
        if  (!PDFFile.exists()  && !PDFFile.isDirectory())
        {
            // System.out.println("//不存在");
            PDFFile.mkdir();
        }
        if  (!ImgFile.exists()  && !ImgFile.isDirectory())
        {
            // System.out.println("//不存在");
            ImgFile.mkdir();
        }
        //如果文件夹不存在则创建
        FileInputStream is= null;
        try {
            is = new FileInputStream(pathPDF+File.separator+fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //上传pdf
        String pdfurl=upDownService.updatePDFHead(fileName,is);
        orderSalesContVo.setOrderSalesContPdf(pdfurl);
        //PDFHelper.pdf2Image(pathPDF+"/"+fileName,pathImg,450);
        //System.out.println(pdfurl);
        //orderSalesContVo.setCreateTime(new Date());

       try {
            orderSalesContMapper.updateOrderSalesCont(orderSalesContVo);
        } catch (Exception e) {
            e.printStackTrace();
            return  ServerResponse.createByStatus(ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getCode(),ResponseCode.ORDERSALE_CONT_INSERT_ERROR.getDesc(),null);
        }
        //setTableContClause(contClauseVos);
        //pdfWriteContClause("pdf/test.pdf","lllll",contClauseVos);
        return ServerResponse.createBySuccess(pdfurl);

    }*/

    /*public static void pdfWriteContClause(String path,String title,List<ContClauseVo> contClauseVos){
        Document document=PdfUtil.pdfInit2( path, title);

        Font fontTitle=PdfUtil.setfont(35);
        Paragraph titlePar=new Paragraph(title);
        titlePar.setAlignment(Element.ALIGN_CENTER);
        titlePar.setFont(fontTitle);
       // titlePar.setPaddingTop(10);
        *//*PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100); // 宽度100%填充
        table.setSpacingBefore(10f); // 前间距
        table.setSpacingAfter(10f); // 后间距
        table.setPaddingTop(2);// 顶部空白区高度
        table.setTotalWidth(360);//表格整体宽度*//*
        try {
            document.add(titlePar);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        document.close();
        // 创建表头
       *//* Cell cell1 = PdfUtil.setTableHeader("编号ID");
        Cell cell2 = PdfUtil.setTableHeader("条款标题");
        Cell cell3 = PdfUtil.setTableHeader("条款内容");*//*

    }*/
    /*public static Table setTableContClause(List<ContClauseVo> contClauseVos){
        Document document=PdfUtil.writeToPdf("pdf/clause.pdf","销售合同11");
        //创建一个有3列的表格
        Table table = null;
        try {
            table = new Table(3);
            table.setBorderWidth(1);
            table.setBorderColor(new Color(0, 0, 255));
            table.setPadding(5);
            table.setSpacing(5);
            // 创建表头
            Cell cell1 = PdfUtil.setTableHeader("编号ID");
            Cell cell2 = PdfUtil.setTableHeader("条款标题");
            Cell cell3 = PdfUtil.setTableHeader("条款内容");
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            // 添加此代码后每页都会显示表头
            table.endHeaders();
            for (int i = 0; i < contClauseVos.size(); i++) {
                Cell celli1 = PdfUtil.setTableHeader(contClauseVos.get(i).getClauseId()+"");
                Cell celli2 = PdfUtil.setTableHeader(contClauseVos.get(i).getTitle());
                Cell celli3 = PdfUtil.setTableHeader(contClauseVos.get(i).getContent());
                table.addCell(celli1);
                table.addCell(celli2);
                table.addCell(celli3);
            }
            document.add(new Paragraph("条款如下：",PdfUtil.setFont()));
            document.add(table);
        } catch (BadElementException e) {
            e.printStackTrace();
        }catch (DocumentException e) {
            e.printStackTrace();
        }

        return  table;

    }*/
}
